<?php
	function addHFBSet($name)
	{
		$name = stripNonAlphaNum($name); 
		if($name != '')
		{
			$insert  = "INSERT INTO ".DB_PERFIX."hfb_sets (`id`, `name`)"; 
			$insert .= "VALUES (NULL, '".$name."');";
			$result = mysql_query($insert);
			if ($result > 0)
			{
				$result = mysql_query("SELECT id FROM ".DB_PERFIX."com_headers WHERE name LIKE '".$_POST['name']."';");
				while($row = mysql_fetch_array($result))
				{
					$item['id'] = $row['id'];
					$item['name'] = $row['name'];
				}
			}
		}
		return $item;
	}
	
	function getHFBSet($id)
	{
		if($id > 0)
		{
			$result = mysql_query("SELECT * FROM ".DB_PERFIX."hfb_sets WHERE id= ".$id.";");
			while($row = mysql_fetch_array($result))
			{
				$item['id'] = $row['id'];
				$item['name'] = $row['name'];
				$item['header'] = $row['header'];
				$item['footer'] = $row['footer'];
				$item['border'] = $row['border'];
				$item['border2'] = $row['border2'];
				$item['button'] = $row['button'];
				$item['content_style'] = $row['content_style'];
				$item['wrap_style'] = $row['wrap_style'];
			}
			return $item;
		}
	}
	
	function getHFBSets($incat=0)
	{
		$i=0;
		if($incat > 0)
		{ $result = mysql_query("SELECT * FROM ".DB_PERFIX."hfb_sets WHERE id IN(SELECT item_id FROM ".DB_PERFIX."admin_hfb_cats_to_items WHERE type=4 AND cat_id=".$incat.");"); }
		else
		{ $result = mysql_query("SELECT * FROM ".DB_PERFIX."hfb_sets;"); }
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['name'] = $row['name'];
			$item[$i]['header'] = $row['header'];
			$item[$i]['footer'] = $row['footer'];
			$item[$i]['border'] = $row['border'];
			$item[$i]['border2'] = $row['border2'];
			$item[$i]['button'] = $row['button'];
			$item[$i]['content_style'] = $row['content_style'];
			$item[$i]['wrap_style'] = $row['wrap_style'];
			$i++;
		}
		return $item;
	}
	
	function updateHFBSet($id, $name, $border, $header, $footer, $button, $border2=-1)
	{
		$update  = 'UPDATE '.DB_PERFIX.'hfb_sets SET ';
		$update .= 'name="'.$name.'", ';
		$update .= 'border='.$border.', ';
		$update .= 'header='.$header.', ';
		$update .= 'footer='.$footer.', ';
		if($border2 > -1)
		{ $update .= ' border2 = '.$border2.', '; }
		$update .= 'button='.$button.' ';
		$update .= 'WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update);
	}
	
	function getSiteModules($site_id, $set_id=0)
	{
		$i=0;
		if($set_id > 0)
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'modules WHERE site_id='.$site_id.' AND set_id='.$set_id.' ORDER BY mod_id ASC;'); }
		else
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'modules WHERE site_id='.$site_id.' ORDER BY mod_id ASC;'); }
		while($row = mysql_fetch_array($result))
		{
			$item[$i]['id'] = $row['id'];
			$item[$i]['set_id'] = $row['set_id'];
			$item[$i]['site_id'] = $row['site_id'];
			$item[$i]['mod_id'] = $row['mod_id'];
			$item[$i]['name_l'] = $row['name_l'];
			$item[$i]['name_r'] = $row['name_r'];
			$item[$i]['path'] = $row['path'];
			$item[$i]['icon'] = $row['icon'];
			$item[$i]['type'] = $row['type'];
			$item[$i]['hfb_set'] = $row['hfb_set'];
			$item[$i]['active'] = $row['active'];
			$item[$i]['max'] = $row['max'];
			$item[$i]['cols'] = $row['cols'];
			$item[$i]['rows'] = $row['rows'];
			$item[$i]['indexing'] = $row['indexing'];
			$i++;
		}
		return $item;
	}
	
	function getModuleAdmin($id)
	{
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."modules WHERE id=".$id.";");
		while($row = mysql_fetch_array($result))
		{
			$item['id'] = $row['id'];
			$item['set_id'] = $row['set_id'];
			$item['site_id'] = $row['site_id'];
			$item['mod_id'] = $row['mod_id'];
			$item['name_l'] = $row['name_l'];
			$item['name_r'] = $row['name_r'];
			$item['icon'] = $row['icon'];
			$item['type'] = $row['type'];
			$item['path'] = $row['path'];
			$item['hfb_set'] = $row['hfb_set'];
			$item['active'] = $row['active'];
			$item['max'] = $row['max'];
			$item['cols'] = $row['cols'];
			$item['rows'] = $row['rows'];
			$item['indexing'] = $row['indexing'];
		}
		if ($item['hfb_set'] > 0)
		{
			$result = mysql_query("SELECT * FROM ".DB_PERFIX."hfb_sets WHERE id=".$item['hfb_set'].";");
			while($row = mysql_fetch_array($result))
			{
					$item['com_border'] = $row['border'];
					$item['com_header'] = $row['header'];
					$item['com_footer'] = $row['footer'];
					$item['com_button'] = $row['button'];
			}
		}
		return $item;
	}
	function newModule($name_l, $name_r)
	{
		$name_l = stripNonAlphaNum($name_l); 
		$name_r = stripNonAlphaNum($name_r);
		if($name_l != '')
		{
			$insert  = "INSERT INTO ".DB_PERFIX."modules (`id`, `name_l`, `name_r`, `path`, `active`)"; 
			$insert .= "VALUES (NULL, '".$name_l."', '".$name_r."', 'modules/', 0);";
			$result = mysql_query($insert);
			return $result;
		}
	}
	
	function updateModule($id, $set_id, $mod_id, $name_l, $name_r, $hfb_set=0, $path='', $type=0, $max=0, $cols=0, $rows=0, $indexing=0)
	{
		$name_l 	= stripNonAlphaNum($name_l); 
		$name_r 	= stripNonAlphaNum($name_r);
		
		if(!($hfb_set > 0))
		{ $hfb_set = 0; }
		if(!($site_template > 0))
		{ $site_template = 0; }
		
		if($name_l != '')
		{
			$update  = ' UPDATE '.DB_PERFIX.'modules SET ';
			$update .= ' `set_id` = '.$set_id.', ';
			$update .= ' `mod_id` = '.$mod_id.', ';
			$update .= ' `name_l` = "'.$name_l.'", ';
			$update .= ' `name_r` = "'.$name_r.'", ';
			if($path != '')
			{ $update .= ' `path` = "'.$path.'", '; }
			$update .= ' `type` = '.$type.', ';
			$update .= ' `max` = '.$max.', ';
			$update .= ' `cols` = '.$cols.', ';
			$update .= ' `rows` = '.$rows.', ';
			$update .= ' `indexing` = '.$indexing.', ';
			$update .= ' `hfb_set` = '.$hfb_set.' ';
			$update .= ' WHERE id='.$id.' LIMIT 1; ';
			$result = mysql_query($update);
		}
	}
	
	function deleteModule($id)
	{
		if($id > 0)
		{
			$result = mysql_query('DELETE FROM '.DB_PERFIX.'modules WHERE id='.$id.' LIMIT 1;');
			return $result;
		}
	}
	
	function addModuleToSite($site, $module)
	{
		if(($module > 0) && ($site > 0))
		{
			$mod = getModuleAdmin($module);
			if($mod['id'] == $module)
			{
				$insert  = "INSERT INTO ".DB_PERFIX."modules (`id`, `set_id`, `site_id`, `mod_id`, `name_l`, `name_r`, `path`, `active`, `max`, `cols`, `rows`, `indexing`)"; 
				$insert .= "VALUES (NULL, ".$mod['set_id'].", ".$site.", ".$mod['mod_id'].", '".$mod['name_l']."', '".$mod['name_r']."', '".$mod['path']."', 1, ".$mod['max'].", ".$mod['cols'].", ".$mod['rows'].", ".$mod['indexing'].");";
				$result = mysql_query($insert);
				return $result;
			}
		}
	}
?>